//
//  ZCSeeBigPictureViewController.m
//  BaiSi
//
//  Created by 张灿 on 16/5/7.
//  Copyright © 2016年 张灿. All rights reserved.
//

#import "ZCSeeBigPictureViewController.h"
#import <UIImageView+WebCache.h>
#import "ZCThemeItem.h"
#import <SVProgressHUD.h>
#import "ZCPhotoManager.h"
#import <Photos/Photos.h>

static NSString * const albumTitle = @"百思不得姐";

@interface ZCSeeBigPictureViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) UIImageView *imageView;
@property (nonatomic, strong) UIImage *image;

@end

@implementation ZCSeeBigPictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.scrollView.backgroundColor = [UIColor blackColor];
    //创建imageview
    UIImageView *imageView = [[UIImageView alloc] init];
    
    //加载图片
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.item.image0] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        _image = image;
    }];
    
    //设置图片尺寸
    CGFloat h = ZCScreenW / self.item.width * self.item.height;
    imageView.frame = CGRectMake(0, 0, ZCScreenW, h);
    
    //判断是不是大图,是大图,设置scroll的contentSize
    if (self.item.is_bigPicture) {
        self.scrollView.contentSize = CGSizeMake(ZCScreenW, h);
    } else {
        imageView.center = CGPointMake(ZCScreenW * 0.5, ZCScreenH * 0.5);
    }
    
    //添加图片
    [self.scrollView addSubview:imageView];
    self.imageView = imageView;
    
    //设置scroll代理
    self.scrollView.delegate = self;
    
    //设置最小缩放比例是1
    self.scrollView.minimumZoomScale = 1;
    
    //如果图片原始高度大于设置的图片高度,设置最大缩放比例
    if (self.item.height > h) {
        self.scrollView.maximumZoomScale = _item.height / h;
    }
}

//让scrollView有缩放功能
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _imageView;
}

//关闭
- (IBAction)closeClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

//保存图片
- (IBAction)saveClick:(id)sender {
    //保存到系统相册
//    UIImageWriteToSavedPhotosAlbum(<#UIImage * _Nonnull image#>, <#id  _Nullable completionTarget#>, <#SEL  _Nullable completionSelector#>, <#void * _Nullable contextInfo#>)
    // completionTarget:保存完成，就会调用target
    // completionSelector:保存完成，就会调用target的completionSelector
    // 注意:completionSelector不能乱写，只能使用系统提供哪个方法
    // UIImageWriteToSavedPhotosAlbum:底层就会自动弹出对话框，询问下用户是否允许当前app访问相册
//    UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    
    
    // bug:每次都创建相册
    
    // 判断下当前app是否允许访问相册
    // 获取app访问相册状态
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    
    if (status == PHAuthorizationStatusAuthorized) {
        
        // 把图片保存到自己相册
        [ZCPhotoManager addImage:_image toAlbum:albumTitle completionHandler:^(BOOL success, NSError *error) {
            if (success) {
                [SVProgressHUD showSuccessWithStatus:@"保存成功"];
            } else {
                [SVProgressHUD showErrorWithStatus:@"保存失败"];
            }
            
        }];
        
    } else if (status == PHAuthorizationStatusNotDetermined) {
        
        // 弹出系统对话框，通知用户是否允许访问相册
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            if (status == PHAuthorizationStatusAuthorized) {
                // 把图片保存到自己相册
                [ZCPhotoManager addImage:_image toAlbum:albumTitle completionHandler:^(BOOL success, NSError *error) {
                    if (success) {
                        [SVProgressHUD showSuccessWithStatus:@"保存成功"];
                    } else {
                        [SVProgressHUD showErrorWithStatus:@"保存失败"];
                    }
                }];
            }
        }];
        
    } else { // 不允许访问相册
        
        // 提示用户，去哪打开访问开关
        // 点击设置，找到当前app,打开照片开关
        [SVProgressHUD showSuccessWithStatus:@"点击设置，找到当前app,打开照片开关，即可保存图片"];
        
    }
    
    /*
     PHAuthorizationStatusNotDetermined = 0, 用户还没有进行选择
     PHAuthorizationStatusRestricted： 不允许访问
     PHAuthorizationStatusDenied,   不允许访问
     PHAuthorizationStatusAuthorized 允许
     */
}

// 保存图片成功就会调用
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error) {
        // 保存失败
        [SVProgressHUD showErrorWithStatus:@"保存失败"];
    } else {
        // 保存成功
        [SVProgressHUD showSuccessWithStatus:@"保存成功"];
    }
}

@end
